<template>
  <div class="dialog-form">
    <el-dialog v-model="formVisible" class="elDialogBox" title="修改执行时间" @close="onClickCloseBtn">
      <!-- 表单内容 -->
      <div class="dialogCenter">
        <el-form ref="queryRef" :model="formData" :rules="rules" label-width="120px">
          <el-form-item label="期望服务时间：" name="estimatedServerTime">
            <el-date-picker v-model="formData.estimatedServerTime" placeholder="" type="datetime"/>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="onClickCloseBtn">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import {ref, watch} from 'vue'
import dayjs from 'dayjs'

const {proxy} = getCurrentInstance();

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  timeData: {
    type: String
  }
})

const emit: Function = defineEmits(['handleClose', 'handleSubmit'])
const resetType = ref('empty')
const form = ref()
const formVisible = ref(false)

const formData = ref({
  estimatedServerTime: ''
})

const rules = {
  estimatedServerTime: [
    {
      required: true,
      message: '期望服务时间为空，请输入期望服务时间',
      trigger: 'change'
    }
  ]
}

watch(
    () => props.visible,
    (val) => {
      formVisible.value = props.visible
      if (val) {
        formData.value.estimatedServerTime = props.timeData
      }
    }
)

// 提交表单
const submitForm = () => {
  proxy.$refs['queryRef'].validate((valid) => {
    let time = null
    if (formData.value.estimatedServerTime !== undefined) {
      time = dayjs(formData.value.estimatedServerTime).format(
          'YYYY-MM-DD HH:mm:ss'
      )
    }
    emit('handleSubmit', {estimatedServerTime: time})
    onClickCloseBtn()
  })
}

const onClickCloseBtn = () => {
  proxy.resetForm('queryRef');
  emit('handleClose')
}
</script>
<style lang="scss" scoped>
.t-popup {
  width: auto;
}
</style>
